﻿using System;
namespace LeRenard.Libraries.Templating.Templating.Mapping
{
    /// <summary>
    /// Implementation of <see cref="IGenericTemplateMappingBuilder"/>,
    /// which defines a generic template mapping builder,
    /// a construct that facilitates the mapping fluent api.
    /// </summary>
    /// <typeparam name="TObject">The type of the object used in the calculation of the value.</typeparam>
    internal class GenericTemplateMappingBuilder<TObject> : NonGenericTemplateMappingBuilder, IGenericTemplateMappingBuilder<TObject>
    {
        /// <summary>
        /// Constructs a new <see cref="GenericTemplateMappingBuilder"/> instance.
        /// </summary>
        /// <param name="template">The template for which we're building a mapping.</param>
        /// <param name="placeholder">The placholder attached to the mapping we're building.</param>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="template"/> is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="placeholder"/> is null, empty or only exists of white spaces.</exception>
        internal GenericTemplateMappingBuilder(ITemplate template, string placeholder)
            : base(template, placeholder)
        {
        }

        /// <summary>
        /// Maps a function, that returns a string, which generates a value for the placeholder,
        /// and adds it to the template.
        /// </summary>
        /// <param name="value">A function that returns a string.</param>
        public void To(Func<TObject, string> value)
        {
            ThrowIf.Null(() => value);

            var mapping = new GenericTemplateMapping<TObject>(this.Placeholder, value);
            this.Template.AddMapping(mapping);
        }
    }
}
